package com.insurance.service.impl;

import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.dao.PartnerInfoDao;
import com.insurance.entity.PartnerInfoEntity;
import com.insurance.service.PartnerInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;
import org.springframework.util.CollectionUtils;


@Service("partnerInfoService")
public class PartnerInfoServiceImpl extends ServiceImpl<PartnerInfoDao, PartnerInfoEntity> implements PartnerInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params, QueryWrapper<PartnerInfoEntity> queryWrapper) {
        IPage<PartnerInfoEntity> page = this.page(new Query<PartnerInfoEntity>().getPage(params),queryWrapper);
        return new PageUtils(page);
    }

    /**
     * 查询列表
     *
     * @param partnerInfoEntity 查询条件
     * @return
     */
    @Override
    public List<PartnerInfoEntity> queryList(PartnerInfoEntity partnerInfoEntity){
        QueryWrapper<PartnerInfoEntity> queryWrapper = getQueryWrapper(partnerInfoEntity);
        return this.list(queryWrapper);
    }

    /**
     * 获取查询条件
     * @return
     */
    @Override
    public QueryWrapper<PartnerInfoEntity> getQueryWrapper(){
        QueryWrapper<PartnerInfoEntity> queryWrapper = new QueryWrapper<PartnerInfoEntity>();
        queryWrapper.eq("delete_flag", DeleteFlagEnum.UN_DELETE.getCode());
        return queryWrapper;
    }

    /**
     * 获取查询条件
     *
     * @param partnerInfoEntity 查询条件
     * @return
     */
    @Override
    public QueryWrapper<PartnerInfoEntity> getQueryWrapper(PartnerInfoEntity partnerInfoEntity){
        QueryWrapper<PartnerInfoEntity> queryWrapper = getQueryWrapper();
        if(!StringUtils.isBlank(partnerInfoEntity.getPartnerName())){
            queryWrapper.like("partner_name", partnerInfoEntity.getPartnerName());
        }
        if(!StringUtils.isBlank(partnerInfoEntity.getPartnerCode())){
            queryWrapper.eq("partner_code", partnerInfoEntity.getPartnerCode());
        }
        if(!StringUtils.isBlank(partnerInfoEntity.getAppId())){
            queryWrapper.like("app_id", partnerInfoEntity.getAppId());
        }
        return queryWrapper;
    }

    /**
     * 通过appId查询合作对象
     *
     * @param appId 账号
     * @return
     */
    @Override
    public PartnerInfoEntity getByAppId(String appId){
        if(StringUtils.isBlank(appId)){
            return null;
        }
        QueryWrapper<PartnerInfoEntity> queryWrapper = getQueryWrapper();
        queryWrapper.eq("app_id", appId);
        PartnerInfoEntity partnerInfoEntity = this.getOne(queryWrapper);
        return partnerInfoEntity;
    }


    /**
     * 校验账号密钥
     *
     * @param appId 账号
     * @param appSecret 密钥
     * @return
     */
    @Override
    public boolean isValidate(String appId, String appSecret){
        if(StringUtils.isBlank(appId) || StringUtils.isBlank(appSecret)){
            return false;
        }
        PartnerInfoEntity partnerInfoEntity = getByAppId(appId);
        if(partnerInfoEntity == null || StringUtils.isBlank(partnerInfoEntity.getAppSecret())){
            return false;
        }
        return appSecret.equals(partnerInfoEntity.getAppSecret());
    }

    /**
     * 通过appId查询并更新登录时间
     *
     * @param appId 账号
     * @return
     */
    @Override
    public PartnerInfoEntity loginByAppId(String appId){
        PartnerInfoEntity partnerInfoEntity = getByAppId(appId);
        if(partnerInfoEntity != null){
            partnerInfoEntity.setLoginTime(new Date());
            //不管更新时间是否成功都返回实体
            this.updateById(partnerInfoEntity);
            return partnerInfoEntity;
        }
        return null;
    }

    /**
     * 通过主键批量删除
     *
     * @param partnerIdList 主键集合
     * @param partnerInfoEntity 更新的值
     * @return
     */
    @Override
    public boolean removeBatch(List<String> partnerIdList, PartnerInfoEntity partnerInfoEntity){
        if(CollectionUtils.isEmpty(partnerIdList)){
            return true;
        }
        partnerInfoEntity.setDeleteFlag(String.valueOf(DeleteFlagEnum.DELETE.getCode()));
        QueryWrapper<PartnerInfoEntity> queryWrapper = getQueryWrapper();
        queryWrapper.in("partner_id", partnerIdList);
        return this.update(partnerInfoEntity, queryWrapper);
    }

    /**
     * 校验合作方编码
     *
     * @param partnerCode 合作方编码
     * @return 没有重复的返回true, 有返回false
     */
    @Override
    public boolean checkPartnerCode(String partnerCode){
        if(StringUtils.isBlank(partnerCode)){
            return false;
        }
        PartnerInfoEntity partnerInfoEntity = new PartnerInfoEntity();
        partnerInfoEntity.setPartnerCode(partnerCode);
        QueryWrapper<PartnerInfoEntity> queryWrapper = getQueryWrapper(partnerInfoEntity);
        return CollectionUtils.isEmpty(this.list(queryWrapper));
    }

    /**
     * 校验合作方appId
     *
     * @param appId 合作方appId
     * @return 没有重复的返回true, 有返回false
     */
    @Override
    public boolean checkPartnerAppId(String appId){
        if(StringUtils.isBlank(appId)){
            return false;
        }
        PartnerInfoEntity partnerInfoEntity = new PartnerInfoEntity();
        partnerInfoEntity.setAppId(appId);
        QueryWrapper<PartnerInfoEntity> queryWrapper = getQueryWrapper(partnerInfoEntity);
        return CollectionUtils.isEmpty(this.list(queryWrapper));
    }

}